app.controller('evaluate-details', function ($scope, $http) {
    var token = localStorage.getItem('token');
    //查看是否点赞（true or false）
    var isLike = sessionStorage.getItem('isLike');
    var dynamicId = sessionStorage.getItem('dynamicId');
    $scope.dynamicInfo = {};
    //找到用户id
    $scope.userInfo = {};
    $scope.interaction = {};
    //评论内容数组
    $scope.interactionInfos = [];
    //评论内容对象
    $scope.interactionInfo = {};
    console.log(token);

    $scope.showDynamic = function (page) {
        if (!dynamicId) {
            $.toast("找不到动态");

            location.href = '#refletion';

            return;
        }
        if (dynamicId) {

            $scope.page = page ? page : 1;

            // 若该加载为初始化加载，即没有传入page参数
            if (!page) {
                $scope.page = 1;
            }


            $http({
                method: 'GET',
                url: app.netPath + '/interaction/show/comments',
                params: {
                    'dynamicId': dynamicId,
                    'page': $scope.page
                }
            }).then(function (res) {
                console.log(res.data);
                var infiniteScroll = $('.infinite-scroll');
                var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                if (!res.data.error) {

                    if (isLike == 1) {
                        $scope.isLikeInfo = 1;
                    } else {

                    }
                    //含有的评论总数
                    $scope.allCommentCount = res.data.allCommentCount;
                    //没有评论的情况
                    if (res.data.data.user) {
                        $scope.dynamicInfo = res.data.data;
                        //判断用户头像是否存在
                        if (res.data.data.user.userAvatarAddress) {
                            $scope.dynamicInfo.user.userAvatarAddress = app.netPath + '/resources/img/' + $scope.dynamicInfo.user.userAvatarAddress;
                            app.buildImageUrl($scope.dynamicInfo.user, 'userAvatarAddress', function () {
                                $scope.$digest();
                            });
                        }
                    } else {
                        //存在动态直接评论时
                        $scope.dynamicInfo = res.data.data[0].dynamic;
                        //判断动态发表用户头像是否存在
                        if (res.data.data[0].dynamic.user.userAvatarAddress) {
                            $scope.dynamicInfo.user.userAvatarAddress = app.netPath + '/resources/img/' + $scope.dynamicInfo.user.userAvatarAddress;
                            app.buildImageUrl($scope.dynamicInfo.user, 'userAvatarAddress', function () {
                                $scope.$digest();
                            });
                        }

                        for (var i = 0; i < res.data.data.length; i++) {
                            if (res.data.data[i].user1.userAvatarAddress) {
                                res.data.data[i].user1.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user1.userAvatarAddress;
                            }
                            //将评论存入数组
                            $scope.interactionInfos.push(res.data.data[i]);
                        }
                    }
                    sessionStorage.setItem('replayId', $scope.dynamicInfo.dynamicUserId);

                    //动态图片
                    $scope.dynamicInfo.imgAddresses = [];
                    if ($scope.dynamicInfo.dynamicImg) {

                        var dynamicImgs = $scope.getDynamicImg($scope.dynamicInfo.dynamicImg);
                        if (dynamicImgs instanceof Array && dynamicImgs.length > 0) {
                            for (var i = 0; i < dynamicImgs.length; i++) {
                                var img = app.netPath + '/resources/dynamicImg/' + dynamicImgs[i];
                                if (dynamicImgs[i] == null || img == null || img == '')
                                    continue;
                                var imgObject = {
                                    imgSrc: img,
                                    isLoad: false
                                };
                                app.buildImageUrl(imgObject, 'imgSrc', function () {
                                    $scope.$digest();
                                });
                                $scope.dynamicInfo.imgAddresses.push(imgObject);
                            }
                        }

                        // var makeItTrue = function (p,img) {
                        //     img.onload = function () {
                        //         $scope.dynamicInfo.imgAddresses[p].isLoad = true;
                        //         $scope.$digest();
                        //     }
                        // };
                        //
                        // for (var p = 0; p < $scope.dynamicInfo.imgAddresses.length; p++) {
                        //     var image = new Image();
                        //     makeItTrue(p,image);
                        //     // image.onload = function () {
                        //     //     $scope.taskInfo.imgAddresses[p].isLoad = true;
                        //     //     $scope.$digest();
                        //     // };
                        //     image.src = $scope.dynamicInfo.imgAddresses[p].imgSrc;
                        // }
                    }

                    $scope.isOnloading = false;

                    // 启用无限加载事件
                    $.attachInfiniteScroll(infiniteScroll);
                    $scope.thispage = $scope.interactionInfos.length % 15 == 0 ? $scope.interactionInfos.length / 15 : $scope.interactionInfos.length / 15 + 1;
                    $scope.thispage = parseInt($scope.thispage);
                    console.log($scope.thispage);
                    if (res.data.allPage <= $scope.thispage) {
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        $.detachInfiniteScroll(infiniteScroll);
                        // 删除加载提示符
                        infiniteScrollPreloader.hide();

                    }
                }
                else {
                    $.toast(res.data.msg);
                    // 加载完毕，则注销无限加载事件，以防不必要的加载
                    $.detachInfiniteScroll(infiniteScroll);
                    // 删除加载提示符
                    infiniteScrollPreloader.hide();
                }
            })
        }
    };

    //动态图片分割（","）
    $scope.getDynamicImg = function (dynamicImgString) {
        var dynamicImg = dynamicImgString.split(',');
        return dynamicImg;
    };
    //点赞
    $scope.addLike = function (dynamicId) {
        if (token) {
            $http({
                method: 'get',
                url: app.netPath + '/dynamic/giveLike',
                headers: {Authorization: token},
                params: {dynamicId: dynamicId}
            }).then(function (res) {
                if (!res.data.error) {
                    $.toast(res.data.msg);
                    $scope.dynamicInfo.dynamicLikeNum += 1;
                    sessionStorage.setItem('isLike', 1);
                    sessionStorage.setItem('dynamicId', dynamicId);
                    location.href = '#evaluate-details'
                } else {
                    $.toast("你已经顶过了");
                }
            });
        } else {
            $.confirm('您还未登录，马上去登陆', function () {
                location.href = '#user-login';
            });
        }

    };


    //评论动态
    $scope.review = function () {

        //判断有无证书
        if (!token) {
            $.toast('请先登录');
            location.href = '#user-login';

            return;
        }

        if (token) {

            $scope.interaction = {
                "dynamicId": $scope.interaction.dynamicId,
                'dynamicComment': $scope.interaction.dynamicComment,
                'interactionImg': ""
            };

            $scope.interaction.dynamicId = $scope.dynamicInfo.dynamicId;
            //$.showPreloader('请稍候');

            if ($scope.interaction.dynamicComment != null && $scope.interaction.dynamicComment != "") {
                $http({
                    method: 'POST',
                    url: app.netPath + '/interaction/add/comment',
                    headers: {Authorization: token},
                    data: $scope.interaction
                }).then(function (res) {


                    console.log(res.data);
                    if (!res.data.error) {
                        $.toast('评论成功！');
                        location.href = '#evaluate-details'
                    }
                    else {
                        $.toast(res.data.msg)
                    }
                })
            } else {
                $.toast("评论内容不能为空！");
            }

        }

    };

    //跳转更多评论
    $scope.reviewMore = function (interactionId) {

        sessionStorage.setItem('interactionId', interactionId);
        location.href = '#evaluate-more'
    }


});